package com.ztc.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ztc.bean.ExcelModel;
import com.ztc.bean.ProjectModel;
import com.ztc.bean.QuoteModel;
import com.ztc.bean.ReportModel;

@Mapper
public interface ReportDao {
	
	@Select("<script>"
			+ "select tp.*,"
			+ "tca.name as projectType,tor.name as projectOwner,tu1.realName manager,"
			+ "group_concat(distinct tpum.userId) memberId,group_concat(distinct tu2.realName) member"
			+ " from tab_project tp left join tab_user tu1 on tp.managerId=tu1.userId"
			+ " left join tab_project_user_map tpum1 on tpum1.projectId=tp.projectId"
			+ " left join tab_project_user_map tpum on tpum.projectId=tp.projectId"
			+ " left join tab_user tu2 on tpum.userId=tu2.userId"
			+ " left join tab_category tca on tca.id=tp.projectTypeId"
			+ " left join tab_organization tor on tor.id=tp.projectOwnerId"
			+ " where 1=1"
			+ "<if test='projectId!=null'>"
			+ " and tp.projectId=#{projectId}"
			+ "</if>"
			+ " group by tp.projectId order by updateTime desc"
			+ "</script>")
	public ProjectModel queryProjectById(@Param("projectId") int projectId);
	
	/**
	 * 查询报告标题
	 * @param authType
	 * @return
	 */
	@Select("<script>"
			+ "select ta.id, ta.name,ta.parentId,ta.sequence,ta.relation,ta.isExport,tr.serialNum,tr.titleLevel,auth_getparentList(ta.id) fullName"
			+ " from tab_auth ta"
			+ " left join tab_relation tr on tr.authId=ta.id"
			+ " where ta.authType in " //,#{authType}3,#{authType}4,#{authType}5
			+ "<foreach collection='authType' item='one' open='(' close=')' separator=','>"
			+ "#{one}"
			+ "</foreach>"
			+ "</script>")
	public List<Map<String, Object>> queryAllReportContent(@Param("authType") List<Integer> authType);

	/**
	 * 查询报告内容
	 * @param authId
	 * @param relation
	 * @param projectId
	 * @return
	 */
	@Select("select ta.id as authId, ta.name as authName,ta.relation,ta.isExport,"
			+ "tr.chName,tr.serialNum,tr.titleLevel,tr.pageNum,tr.template,tr.tips,"
			+ "ztc.contentId,ztc.content,ztc.isExport as isExportForProject,ztc.updateTime"
			+ " from tab_auth ta"
			+ " left join tab_relation tr on tr.authId=ta.id"
			+ " left join ztc_${relation} ztc on 1=1 and ztc.projectId=#{projectId}"
			+ " where ta.id=#{authId} limit 1")
	public ReportModel queryReportContent(@Param("authId") int authId, @Param("relation") String relation, @Param("projectId") int projectId);
	
	/**
	 * 查询模板
	 * @param authId
	 * @return
	 */
	@Select("select tr.template from tab_relation tr where tr.authId=#{authId}")
	public ReportModel queryReportTemplate(@Param("authId") int authId);
	
	/**
	 * 查询模板
	 * @param authId
	 * @return
	 */
	@Select("select tr.template from tab_relation tr where tr.tableId=#{relation}")
	public List<ReportModel> queryTemplateByRelation(@Param("relation") String relation);
	
	/**
	 * 查询校验正则
	 * @param authId
	 * @return
	 */
	@Select("select regex from tab_relation where authId=#{authId}")
	public ReportModel queryRegex(@Param("authId") int authId);
	
	/**
	 * 根据条件查询报告内容
	 * @param reportModel
	 * @return
	 */
	@Select("<script>select * from ztc_${reportModel.relation} where 1=1"
			+ "<if test='reportModel.projectId != null and reportModel.projectId!=\"\"'>"
			+ " and projectId=#{reportModel.projectId}"
			+ "</if>"
			+ "<if test='reportModel.updateTime != null'>"
			+ " and updateTime=#{reportModel.updateTime}"
			+ "</if>"
			+ "</script>")
	public ReportModel queryReportContentByCon(@Param("reportModel") ReportModel reportModel);
	
	/**
	 * 添加报告内容
	 * @param reportModel
	 * @return
	 */
	@Insert("insert into ztc_${reportModel.relation} (content,pureContent,projectId,isExport,enterpriseId,version) values "
			+ "(#{reportModel.content},#{reportModel.pureContent},#{reportModel.projectId},#{reportModel.isExport},#{reportModel.enterpriseId},#{reportModel.version})")
	public int addReportContent(@Param("reportModel") ReportModel reportModel);
	
	/**
	 * 修改报告内容
	 * @param reportModel
	 * @return
	 */
	@Update("update ztc_${reportModel.relation} set content=#{reportModel.content},pureContent=#{reportModel.pureContent},version=#{reportModel.version} where projectId=#{reportModel.projectId}")
	public int updateReportContent(@Param("reportModel") ReportModel reportModel);
	
	/**
	 * 修改是否导出
	 * @param isExport
	 * @return
	 */
	@Update("update ztc_${reportModel.relation} set isExport=#{reportModel.isExport} where projectId=#{reportModel.projectId}")
	public int updateIsExport(@Param("reportModel") ReportModel reportModel);
	
	/**
	 * 查询权限对应的引用规则
	 * @param authId
	 * @return
	 */
	@Select("select * from tab_quote where authId=#{authId} order by sequence")
	public List<QuoteModel> queryQuoteList(int authId);
	
	/**
	 * 查询项目明细表内容
	 * @param relation
	 * @param projectId
	 * @return
	 */
	@Select("select * from ztc_${relation} where projectId=#{projectId}")
	public List<ExcelModel> queryExcel(@Param("relation") String relation, @Param("projectId") int projectId);
	
	@Select("select auth_getParentList(#{authId}) as fullName from dual")
	public Map<String, Object> queryAuthParentList(@Param("authId") int authId);
}
